/* 
 * File:   FileHeader.h
 * Author: Enrique Antezana
 *
 * Created on 14 de mayo de 2013, 04:44 PM
 */

#ifndef FILEHEADER_H
#define	FILEHEADER_H

#include <string>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>

struct CentralDirectory;

const int COMMON_FILE_HEADER_FIELDS_LENGHT = 30;
const unsigned int FILE_HEADER_SIGNATURE = 0x04034b50;

class FileHeader
{
public:

    int headerSignature_;
    short versionNedded_;
    short flag_;
    short compressionMethod_;
    unsigned short lastModifiedTime_;
    unsigned short lastModifiedDate_;
    unsigned int crc_;
    int compressedSize_;
    int unCompressedSize_;
    short fileNameLength_;
    short extraFieldLength_;
    char* fileName_;
    char* extraField_;

    short size_;
    char* fileData_;
    CentralDirectory* centralDirectory_;

public:

    FileHeader();
    FileHeader(const char* path);
    ~FileHeader();
    bool operator ==(const FileHeader& fileHeader);
    void setFileName(const char* name, unsigned int nameLength);
    void setExtraField(const char* extraField, unsigned int extraFieldLength);
    void setContent(const char* content, unsigned int contentSize);
    void setCentralDirectory(CentralDirectory* centralDirectory);
    int calculateSize();
    float calculateCompressionPercentage();
    std::string parseTimeToString();
    unsigned long calculateCRC();
    short parseTimeToShort(tm* time);
    short parseDateToShort(tm* time);


private:
    tm * parseTimeAndDate();
    void releaseMemory(char* ptr);
};

std::ostream& operator <<(std::ostream& out, FileHeader& fileHeader);

#endif	/* FILEHEADER_H */
